/**
 * 
 */
package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.Eye;
import com.mvc.service.healthmanager.EyeServiceI;

/**
 * 眼睛事物控制器
 * @author qingjie
 *
 */
@RequestMapping("/eye")
@Controller
public class EyeController {
	
	@Autowired
	private EyeServiceI esi;
	
	@RequestMapping("/saveEye")
	@ResponseBody
	public Json saveEye(HttpServletRequest request,Eye eye) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
//		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		Eye e = new Eye();
		if (!json.isEmpty()) {
			e = JSON.parseObject(json, Eye.class);
			e.setId(null);
		}
		eye = esi.saveOrUpdate(e);
		Json j = new Json();
		if(eye!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(eye);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping(value="/saveEye", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveEye(@RequestBody List<Eye> es,HttpServletRequest request) throws Exception{
		System.out.println("未转化的数据："+JSON.toJSONString(es));
		List<Eye> list = new ArrayList<Eye>();
		Json j = new Json();
		if(es.size()>0){
			for(Eye e : es){
				Long appLocalId = ValidatorUtil.isNullOnLong(e.getId())?null:e.getId();
				e.setId(null);
				Eye eye = new Eye();
				eye = esi.saveOrUpdate(e);
				if(eye!=null&&!ValidatorUtil.isNullOnLong(eye.getId())){
					eye.setRespCode(0);
				}else{
					eye.setRespCode(-1);
				}
				eye.setAppLocalId(appLocalId);
				list.add(eye);
			}
			j.setMsg("数据已处理");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空");
			j.setSuccess(false);
		}
		return j;
	}
}
